package com.wnx.job.modules.nms.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.wnx.job.common.api.PageResult;
import com.wnx.job.common.api.QueryPageBean;
import com.wnx.job.common.exception.Asserts;
import com.wnx.job.common.mapstruct.ArticleAttachmentMapper;
import com.wnx.job.common.mapstruct.ArticleMapper;
import com.wnx.job.common.utils.StrUtils;
import com.wnx.job.modules.nms.dto.ArticleAttachmentParam;
import com.wnx.job.modules.nms.dto.ArticleParam;
import com.wnx.job.modules.nms.mapper.NmsArticleMapper;
import com.wnx.job.modules.nms.model.NmsArticle;
import com.wnx.job.modules.nms.model.NmsArticleAttachment;
import com.wnx.job.modules.nms.service.NmsArticleAttachmentService;
import com.wnx.job.modules.nms.service.NmsArticleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-16
 */
@Service
public class NmsArticleServiceImpl extends ServiceImpl<NmsArticleMapper, NmsArticle> implements NmsArticleService {
    @Resource
    private NmsArticleMapper articleMapper;
    @Resource
    private NmsArticleAttachmentService articleAttachmentService;
    /**
     * 保存咨询
     * @param articleParam
     */
    @Override
    public void saveArticle(ArticleParam articleParam) {

        //属性复制
        NmsArticle tbArticle = ArticleMapper.INSTANCE.toPojo(articleParam);

        //调用持久层，进行新闻咨询新增
        tbArticle.setCreated(new Date());
        articleMapper.insert(tbArticle);


        //获取新闻咨询的ID
        long articleId = tbArticle.getId();

        //从新闻咨询中获取附件对象
        ArticleAttachmentParam articleAttachmentParam = articleParam.getArticleAttachmentParam();
        //判读附件对象是否不为空？
        if (    articleAttachmentParam != null &&
                StringUtils.isNotBlank(articleAttachmentParam.getName()) &&
                StringUtils.isNotBlank(articleAttachmentParam.getUrl())
        ){

            //添加新闻咨询附件。
            NmsArticleAttachment attachment = ArticleAttachmentMapper.INSTANCE.toPojo(articleAttachmentParam);
            attachment.setArticleId(articleId);
            articleAttachmentService.save(attachment);
        }




    }
    /**
     * 删除新闻咨询
     * @param id
     */
    @Override
    public void deleteArticle(Long id) {
        //删除咨询前，要确保关联的咨询附件删除完毕
        long count = articleAttachmentService.findCountByArticleId(id);
        if(count > 0){ Asserts.fail("该新闻咨询还有新闻咨询附件，请先删除新闻咨询附件");}

        //调用持久层，进行新闻咨询删除。
        articleMapper.deleteById(id);


    }
    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult<NmsArticle> findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<NmsArticle> articles =  articleMapper.findByCondition(queryPageBean.getQueryParams());
        articles.forEach(item->{item.setTitle(StrUtils.CutString(item.getTitle()));});
        return PageResult.restPage(articles);
    }

    /**
     * 根据栏目ID查询咨询数量
     * @param channelId
     * @return
     */
    @Override
    public long findCountByChannelId(Long channelId) {
        return articleMapper.selectCount(new QueryWrapper<NmsArticle>().eq("channel_id",channelId));
    }
}
